package com.lgl.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lgl.constants.SystemConstants;
import com.lgl.dto.AddCategoryDto;
import com.lgl.dto.CategoryDto;
import com.lgl.dto.UpdateCategoryDto;
import com.lgl.entity.Article;
import com.lgl.entity.Category;
import com.lgl.entity.CategoryExcel;
import com.lgl.entity.ResponseResult;
import com.lgl.mapper.CategoryMapper;
import com.lgl.service.ArticleService;
import com.lgl.service.CategoryService;
import com.lgl.utils.BeanCopyUtils;
import com.lgl.utils.WebUtils;
import com.lgl.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private ArticleService articleService;
    @Lazy
    @Autowired
    private CategoryService categoryService;

    @Override
    public ResponseResult getCategoryList() {
        //       查询文章表 判断是否为已发布的文章
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        List<Article> articleList = articleService.list(queryWrapper);
        //获取文章的分类id，并且去重
        Set<Long> categoryIds = articleList.stream()
                .map(article -> article.getCategoryId())
                .collect(Collectors.toSet());
//        查询分类表，判断状态是否正常
        List<Category> categoryList = categoryService.listByIds(categoryIds);
        categoryList = categoryList.stream().
                filter(category -> SystemConstants.STATUS_NORMAL.equals(category.getStatus()))
                .collect(Collectors.toList());
//        封装vo 并返回
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(categoryList, CategoryVo.class);
        return ResponseResult.okResult(categoryVos);
    }

    @Override
    public ResponseResult listAllCategory() {
        List<Category> categories = list();
        List<ListAllCategoryVo> listAllCategoryVos = BeanCopyUtils.copyBeanList(categories, ListAllCategoryVo.class);

        return ResponseResult.okResult(listAllCategoryVos);
    }

    @Override
    public ResponseResult getCategoryList(Integer pageNum, Integer pageSize, CategoryDto categoryDto) {
        Page<Category> categoryPage = new Page<>();
        categoryPage.setSize(pageSize);
        categoryPage.setCurrent(pageNum);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(categoryDto.getName()),Category::getName,categoryDto.getName());
        queryWrapper.eq(StringUtils.hasText(categoryDto.getStatus()),Category::getStatus,categoryDto.getStatus());;
        Page<Category> page = page(categoryPage, queryWrapper);
        List<Category> records = page.getRecords();
        List<GetCategoryListVo> getCategoryListVos = BeanCopyUtils.copyBeanList(records, GetCategoryListVo.class);
        return ResponseResult.okResult(new PageVo(getCategoryListVos,page.getTotal()));
    }

    @Override
    public ResponseResult addCategory(AddCategoryDto addCategoryDto) {
        Category category = BeanCopyUtils.beanCopy(addCategoryDto, Category.class);
        save(category);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getCategory(Long id) {
        Category category = getById(id);
        GetCategoryVo getCategoryVo = BeanCopyUtils.beanCopy(category, GetCategoryVo.class);
        return ResponseResult.okResult(getCategoryVo);
    }

    @Override
    public ResponseResult updateCategory(UpdateCategoryDto updateCategoryDto) {
        Category category = BeanCopyUtils.beanCopy(updateCategoryDto, Category.class);
        updateById(category);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteCategory(Long id) {
        removeById(id);
        return ResponseResult.okResult();
    }


}

